﻿using System.Text.RegularExpressions;

namespace ciji.Core
{
    public class IdCardHelper
    {

        public static int GetAgeFromIdCard(string idcard)
        {
            try
            {
                idcard = idcard.Trim();
                if (idcard.Length == 18)
                {
                    var birthyear = idcard.Substring(6, 4);
                    int fbirthyear = int.Parse(birthyear);
                    int age = DateTime.Now.Year - fbirthyear;
                    return age;
                }
                else if (idcard.Length == 15)
                {
                    var birthyear = "19" + idcard.Substring(6, 2);
                    int fbirthyear = int.Parse(birthyear);
                    int age = DateTime.Now.Year - fbirthyear;
                    return age;
                }
                else
                {
                    return -1;
                }

            }
            catch (Exception ex)
            {

                return -1;
            }
        }
        public static string GetBirthDayFromIdCard(string idcard)
        {
            try
            {
                idcard = idcard.Trim();
                if (idcard.Length == 18)
                {
                    var birthday = idcard.Substring(6, 4) + "-" + idcard.Substring(10, 2) + "-" + idcard.Substring(12, 2);
                    return birthday;
                }
                else if (idcard.Length == 15)
                {
                    var birthday = "19" + idcard.Substring(6, 2) + "-" + idcard.Substring(8, 2) + "-" + idcard.Substring(10, 2);
                    return birthday;
                }
                else
                {

                    return "";
                }

            }
            catch (Exception ex)
            {
                return "";
            }
        }
        public static string GetSexFromIdCard(string idcard)
        {
            try
            {
                idcard = idcard.Trim();
                if (idcard.Length == 18)
                {
                    var sexnum = idcard.Substring(16, 1);
                    var num = 0;
                    int.TryParse(sexnum, out num);
                    if (num % 2 == 1)
                    {
                        return "男";
                    }
                    else
                    {
                        return "女";
                    }

                }
                else if (idcard.Length == 15)
                {
                    var sexnum = idcard.Substring(14, 1);
                    var num = 0;
                    int.TryParse(sexnum, out num);
                    if (num % 2 == 1)
                    {
                        return "男";
                    }
                    else
                    {
                        return "女";
                    }
                }
                else
                {

                    return "";
                }

            }
            catch (Exception ex)
            {
                return "";
            }
        }
        public static dynamic AddressResolution(string address)
        {
            string regex = "(?<province>[^省]+自治区|.*?省|.*?行政区|.*?市)?(?<city>[^市]+自治州|.*?地区|.*?行政单位|.+盟|市辖区|.*?市|.*?县)?(?<county>[^县]+县|.+区|.+市|.+旗|.+海域|.+岛)?(?<town>[^区]+区|.+镇)?(?<village>.*)";

            var m = Regex.Match(address, regex, RegexOptions.IgnoreCase);

            var province = m.Groups["province"].Value;
            var city = m.Groups["city"].Value;
            var county = m.Groups["county"].Value;
            var town = m.Groups["town"].Value;
            var village = m.Groups["village"].Value;
            return new
            {
                Province = province,
                City = city,
                County = county,
                Town = town,
                Village = village
            };


        }
    }
}
